﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Model.Vo;
using Sunny.Service;
using Sunny.Util.Attributes;
using Sunny.Util.Enums;

namespace Sunny.Api.Controllers
{
    [ApiController]
    [Route("/system/role")]
    public class SysRoleController
    {
        private readonly RoleService _roleService;

        public SysRoleController(RoleService roleService)
        {
            _roleService = roleService;
        }

        #region 角色列表
        [HasPermission("system:role:list")]
        [HttpGet("list")]
        public async Task<Result> List([FromQuery]RoleListParam param)
        {
            (List<SysRole> roles,int total) = await _roleService.List(param);
            return Result.Success(new 
            {
                rows = roles,
                total
            });
        }

        #endregion

        #region 修改状态
        [HasPermission("system:role:edit")]
        [LogInfo(Title = "用户管理",BusinessType = BusinessType.Update)]
        [HttpPut("changeStatus")]
        public async Task<Result> ChangeStatus([FromBody]RoleChangeStatusParam param)
        {
            await _roleService.ChangeStatus(param);
            return Result.Success();
        }
        #endregion

        #region 添加角色
        [HasPermission("system:role:add")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Insert)]
        [HttpPost]
        public async Task<Result> Add([FromBody]RoleAddParam param)
        {
            await _roleService.Add(param);
            return Result.Success();
        }

        #endregion

        #region 查询角色详情
        [HasPermission("system:role:query")]
        [HttpGet("{roleId}")]
        public async Task<Result> Detail([FromRoute]long roleId)
        {
            SysRole sysRole  = await _roleService.GetById(roleId);
            return Result.Success(sysRole);
        }
        #endregion

        #region 修改角色
        [HasPermission("system:role:edit")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Update)]
        [HttpPut]
        public async Task<Result> Edit([FromBody]RoleEditParam param)
        {
            await _roleService.Update(param);
            return Result.Success();
        }

        #endregion
        
        #region 删除角色
        [HasPermission("system:role:remove")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Delete)]
        [HttpDelete("{roleId}")]
        public async Task<Result> Edit([FromRoute]long roleId)
        {
            await _roleService.Delete(roleId);
            return Result.Success();
        }

        #endregion

        #region 修改用户数据权限
        [HasPermission("system:role:edit")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Update)]
        [HttpPut("dataScope")]
        public async Task<Result> ChangeDataScope([FromBody]RoleDataScopeEditParam param)
        {
            await _roleService.ChangeDataScope(param);
            return Result.Success();
        }

        #endregion

        #region 查询角色下已授权用户
        [HttpGet("authUser/allocatedList")]
        public async Task<Result> AuthUser([FromQuery]RoleAuthUserParam param)
        {
            (List<SysUser> users,int total) = await _roleService.AuthUser(param);
            return Result.Success(new
            {
               rows = users,
               total
            });
        }

        #endregion
        
        #region 查询角色未已授权用户
        [HttpGet("authUser/unallocatedList")]
        public async Task<Result> NoAuthUser([FromQuery]RoleAuthUserParam param)
        {
            (List<SysUser> users,int total) = await _roleService.NoAuthUser(param);
            return Result.Success(new
            {
                rows = users,
                total
            });
        }

        #endregion

        #region 给角色授权用户
        [HasPermission("system:role:edit")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Update)]
        [HttpPut("authUser/selectAll")]
        public async Task<Result> AuthUserSelectAll([FromBody]RoleAuthUserEditParam param)
        {
            await _roleService.AuthUserAll(param);
            return Result.Success();
        }

        #endregion
        
        #region 给角色取消授权用户
        [HasPermission("system:role:edit")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Update)]
        [HttpPut("authUser/cancelAll")]
        public async Task<Result> AuthUserCancelAll([FromBody]RoleAuthUserEditParam param)
        {
            await _roleService.UnAuthUserAll(param);
            return Result.Success();
        }

        #endregion
        
        #region 给角色取消授权用户-单个用户
        [HasPermission("system:role:edit")]
        [LogInfo(Title = "角色管理",BusinessType = BusinessType.Update)]
        [HttpPut("authUser/cancel")]
        public async Task<Result> AuthUserCancel([FromBody]RoleAuthUserEditParam param)
        {
            await _roleService.UnAuthUserAll(param);
            return Result.Success();
        }

        #endregion
    }
}